package com.jxc.dao.impl;

import java.util.List;

import javax.annotation.Resource;
import java.lang.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.springframework.transaction.annotation.Transactional;

import com.jxc.dao.TbRoleDAO;
import com.jxc.entity.TbRole;

/**
 * Data access object (DAO) for domain model class TbRole.
 * 
 * @see com.TbRole
 * @author MyEclipse Persistence Tools
 */

@Transactional
public class TbRoleDAOImpl implements TbRoleDAO  {
	@Resource private SessionFactory sessionFactory;

	private static final Log log = LogFactory.getLog(TbRoleDAOImpl.class);
	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbRoleDAO#save(com.jxc.entity.TbRole)
	 */
	public void save(TbRole transientInstance) {
		log.debug("saving TbRole instance");
		try {
			sessionFactory.getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbRoleDAO#delete(com.jxc.entity.TbRole)
	 */
	public void delete(TbRole persistentInstance) {
		log.debug("deleting TbRole instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbRoleDAO#findById(java.lang.Integer)
	 */
	public TbRole findById(java.lang.Integer id) {
		log.debug("getting TbRole instance with id: " + id);
		try {
			TbRole instance = (TbRole) sessionFactory.getCurrentSession().get(TbRole.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbRoleDAO#findByExample(com.jxc.entity.TbRole)
	 */
	public List findByExample(TbRole instance) {
		log.debug("finding TbRole instance by example");
		try {
			List results = sessionFactory.getCurrentSession().createCriteria(TbRole.class).add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	/*
	 *分页查询__一页显示的记录
	 * @see com.jxc.dao.impl.TbRoleDAO#findByExample(com.jxc.entity.TbRole)
	 */
	 public List findPageAll(int start,int limit){ 
		 log.debug("findPageAll-------finding TbRole instance by example");
		 try {
				String queryString = "from TbRole u order by u.roleId";
				Query queryObject = sessionFactory.getCurrentSession().createQuery(queryString);
				queryObject.setFirstResult(start*limit); 
				queryObject.setMaxResults(limit); 
			    return queryObject.list();  
			} catch (RuntimeException re) {
				log.error("findPageAll--------finding TbRole", re);
				throw re;
			}
	 } 
	 
	 public  List findByIdByWhere(String strWhere)
	 {
		 log.debug("findPageAll-------finding TbRole instance by example");
		 try {
			 String queryString = "from TbRole u ";
			 if (!strWhere.equals("")) {
				 if (strWhere.equals("1")) {//1:admin
						// HQL=HQL+" AND a.tbRole.roleId=23 or a.tbRole.roleId=2 or
						// a.tbRole.roleId=3 or a.tbRole.roleId=4 or
						// a.tbRole.roleId=1 or a.tbRole.roleId=5 or
						// a.tbRole.roleId=6 ";
						

					} else {
						if(strWhere.equals("5"))// 5:省分司
						{
							queryString+=" where  u.roleId in (6,23,2,3,4) ";
						}
					    else if (strWhere.equals("6")) // 地市公司
						{
					    	queryString+=" where u.roleId in (23,2,3,4)";
						} else if (strWhere.equals("23")) // 县分司
						{
							queryString+=" where u.roleId in (2,3,4)";
						} else {
							queryString+=" where  u.roleId= '" +strWhere+ "  ' ";
						}
					}
			 }
			queryString+= "	 order by u.roleId	";
			Query queryObject = sessionFactory.getCurrentSession().createQuery(queryString);
			    return queryObject.list();  
			} catch (RuntimeException re) {
				log.error("findPageAll--------finding TbRole", re);
				throw re;
			}
	 }
	 /*
      *分页查询__得到记录总条数
	  * @see com.jxc.dao.impl.TbRoleDAO#findByExample(com.jxc.entity.TbRole)
	  */
	public int totalRecord(){ 
		log.debug("totalRecord-------finding TbRole instance by example");
		int count = 0;  
		try {
				String queryString = "select count(n.roleId) from TbRole n";
				Query queryObject = sessionFactory.getCurrentSession().createQuery(queryString);
				count=((Long)queryObject.uniqueResult()).intValue();
				} catch (RuntimeException re) {
					log.error("totalRecord--------finding TbRole", re);
					throw re;
				}
		return 	count;	
	}  
		 

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbRoleDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TbRole instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TbRole as model where model.name= ?";
			Query queryObject = sessionFactory.getCurrentSession().createQuery(queryString);
			System.out.print(value+"----------------"+propertyName);
			queryObject.setParameter(0, value);
	
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbRoleDAO#findByRoleCompetence(java.lang.Object)
	 */
	public List findByRoleCompetence(Object roleCompetence) {
		return findByProperty(ROLE_COMPETENCE, roleCompetence);
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbRoleDAO#findByName(java.lang.Object)
	 */
	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbRoleDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all TbRole instances");
		try {
			String queryString = "from TbRole";
			Query queryObject = sessionFactory.getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbRoleDAO#merge(com.jxc.entity.TbRole)
	 */
	public TbRole merge(TbRole detachedInstance) {
		log.debug("merging TbRole instance");
		try {
			TbRole result = (TbRole) sessionFactory.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbRoleDAO#attachDirty(com.jxc.entity.TbRole)
	 */
	public void attachDirty(TbRole instance) {
		log.debug("attaching dirty TbRole instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbRoleDAO#attachClean(com.jxc.entity.TbRole)
	 */
	public void attachClean(TbRole instance) {
		log.debug("attaching clean TbRole instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	public void delete(Integer id){
		log.debug("saving Actionurl instance");
		try {
			sessionFactory.getCurrentSession().delete(
					sessionFactory.getCurrentSession().load(TbRole.class, id));
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
}